﻿using DTOs;
using RBAC.Domain.Service.Interfae;
using RBAC.Repository.Interfae;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace RBAC.Domain.Service.Impl
{
    public class UserRoleService : IUserRoleService
    {
        /// <summary>
        /// 用户与角色仓储
        /// </summary>
        protected readonly IUserRoleRepository _UserRoleRepository;
        /// <summary>
        /// 角色仓储
        /// </summary>
        protected readonly IRoleRepository _roleRepository;
        /// <summary>
        /// 用户仓储
        /// </summary>
        protected readonly IUserRepository _userRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="UserRoleRepository"></param>
        /// <param name="roleRepository"></param>
        /// <param name="userRepository"></param>
        public UserRoleService(IUserRoleRepository UserRoleRepository, IRoleRepository roleRepository, 
            IUserRepository userRepository)
        {
            _UserRoleRepository = UserRoleRepository;
            _roleRepository = roleRepository;
            _userRepository = userRepository;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="userRole"></param>
        /// <returns></returns>
        public async Task<int> AddUserRoles(int rid, List<int> uid)
        {

            foreach (var item in uid)
            {
                var ter = new UserRole();
                ter.userInfoId = item;
                ter.roleId = rid;
                await _UserRoleRepository.AddAsync(ter);
            }
            return 1;


        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteUserRoles(int id)
        {
            var dels= await _UserRoleRepository.GetModelsAsync(id);
            dels.IsDel = true;
            return await _UserRoleRepository.UpdateAsync(dels);

        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        public async Task<List<UserRoleDto>> GetUserRole(int Rid)
        {
            var GetUserRoleList = (from a in await _UserRoleRepository.GetAllAsync()
                                   join b in await _roleRepository.GetAllAsync()
                                   on a.roleId equals b.roleId
                                   join c in await _userRepository.GetAllAsync()
                                   on a.userInfoId equals c.userInfoId
                                   where !a.IsDel&& b.roleId == Rid
                                   select new UserRoleDto
                                   {
                                       userInfoId = c.userInfoId,
                                       userName = c.userName,
                                       IsDel = c.IsDel,
                                       departmentId = c.departmentId,
                                       roleId = b.roleId,
                                       roleName = b.roleName,
                                       userRoleId = a.userRoleId

                                   });
            return GetUserRoleList.ToList();

        }

        /// <summary>
        /// 反填员工
        /// </summary>
        /// <param name="Ridd"></param>
        /// <returns></returns>
        public async Task<List<UserRoleDto>> GetUserRoleList(int Ridd)
        {
            var GetUserRoleList = (from a in await _UserRoleRepository.GetAllAsync()
                                   join b in await _roleRepository.GetAllAsync()
                                   on a.roleId equals b.roleId
                                   join c in await _userRepository.GetAllAsync()
                                   on a.userInfoId equals c.userInfoId
                                   where !a.IsDel&& b.roleId == Ridd
                                   select new UserRoleDto
                                   {
                                       userInfoId = c.userInfoId,
                                       userName = c.userName,
                                       IsDel = c.IsDel,
                                       departmentId = c.departmentId,
                                       roleId = b.roleId,
                                       roleName = b.roleName,
                                       userRoleId = a.userRoleId

                                   });
            return GetUserRoleList.ToList();
        }
        /// <summary>
        /// 显示用户与角色
        /// </summary>
        /// <returns></returns>
        public async  Task<List<UserRole>> userRolesList(int uid)
        {
            return await _UserRoleRepository.GetAsync(d => d.userInfoId == uid);
        }
    }
}
